package org.dftproject.gedcom.core;

import java.util.Set;

import org.dftproject.genesis.data.genealogy.GenealogyConstants;
import org.dftproject.genesis.data.genealogy.IPerson;
import org.dftproject.genesis.data.genealogy.IRole;
import org.dftproject.genesis.data.genealogy.impl.RoleImpl;

public class BirthEvent extends Event {
	
	private final GedcomPerson child;
	private final IGedcom gedcom;
	private final String famcId;
	
	private boolean rolesInit = false;

	public BirthEvent(Line eventRecord, GedcomPerson child, IGedcom gedcom, String famcId) {
		super(eventRecord);
		this.child = child;
		this.gedcom = gedcom;
		this.famcId = famcId;
	}
	
	@Override
	public Set<IRole> getRoles() {
		if (!rolesInit)
			initRoles();
		return super.getRoles();
	}
	
	@Override
	public Set<IRole> getRoles(String type) {
		if (!rolesInit)
			initRoles();
		return super.getRoles(type);
	}
	
	@Override
	public Set<IRole> getRoles(String[] types) {
		if (!rolesInit)
			initRoles();
		return super.getRoles(types);
	}
	
	protected void initRoles() {
		rolesInit = true;
		
		addRole(new RoleImpl(GenealogyConstants.Child, child, this));
		
		if (famcId != null) {
			Family family = gedcom.getFamily(famcId);
			if (family != null) {
				IPerson father = family.getFather();
				if (father != null)
					addRole(new RoleImpl(GenealogyConstants.Father, father, this));
				
				IPerson mother = family.getMother();
				if (mother != null)
					addRole(new RoleImpl(GenealogyConstants.Mother, mother, this));
			}
		}
	}

}
